
/**
 * 深拷贝
 * @param {Object} source
 * @returns {Object}
 */
export function deepClone(source) {
  if (!source && typeof source !== 'object') {
    throw new Error('error arguments', 'deepClone')
  }
  const targetObj = source.constructor === Array ? [] : {}
  Object.keys(source).forEach(keys => {
    if (source[keys] && typeof source[keys] === 'object') {
      targetObj[keys] = deepClone(source[keys])
    } else {
      targetObj[keys] = source[keys]
    }
  })
  return targetObj
}

/**
 * 自定义时间格式化
 * @param any time 
 * @param string reg 
 */
export const formatTime = (time, reg) => {
  if (!time) return '-'
	
	let date
	if (typeof time === 'object') {
		date = time
	} else if (typeof time === 'string') {
		date = new Date(time.replace(/-/g, '/'))
	} else if (typeof time === 'number') {
		date = new Date(time)
	} else {
		date = new Date()
	}
	
  // const date = typeof time === 'string' ? new Date(time.replace(/-/g, '/')) : time
  const map = {}
  map.yyyy = date.getFullYear()
  map.yy = ('' + map.yyyy).substr(2)
  map.M = date.getMonth() + 1
  map.MM = formatNumber(map.M)
  map.d = date.getDate()
  map.dd = formatNumber(map.d)
  map.H = date.getHours()
  map.HH = formatNumber(map.H)
  map.m = date.getMinutes()
  map.mm = formatNumber(map.m)
  map.s = date.getSeconds()
  map.ss = formatNumber(map.s)
	map.AMPM = map.H > 12 ? 'PM' : 'AM'
  return reg.replace(/\byyyy|yy|MM|M|dd|d|HH|H|mm|m|ss|s|AMPM\b/g, $1 => map[$1])
}

/**
 * 个数补零
 * @param {number} num 
 * @return {string}
 */
export const formatNumber = (num) => {
  num = num + ''
  return num[1] ? num : `0${num}`
}

/**
 * description: 睡眠
 * author: 83
 * createTime: 2021-12-28 11:42:08 ?F10: AM?
 */
export const sleep = (ms = 2000) => {
  return new Promise((resolve) => {
    setTimeout(resolve, ms)
  })
}

/**
 * 生成一个用不重复的ID
 */
export const getUUId = (randomLength = 11) => {
  return 'b' + Number(Math.random().toString().substr(2, randomLength) + Date.now()).toString(36)
}

/**
 * 将日期转换为人类可读的字符串，例如“2小时前”或“3个月前”：
 * @param {Object} date
 */
export function timeSince(date, empty = '') {
	if (!date) return empty
	
	if (typeof date === 'string') {
		date = new Date(date.replace(/-/g, '/'))
	}
	
  const seconds = Math.floor((new Date().getTime() - new Date(date).getTime()) / 1000);

  let interval = Math.floor(seconds / 31536000);
  if (interval >= 1) {
    return interval + "年前";
  }
  interval = Math.floor(seconds / 2592000);
  if (interval >= 1) {
    return interval + "月前";
  }
  interval = Math.floor(seconds / 86400);
  if (interval >= 1) {
    return interval + "天前";
  }
  interval = Math.floor(seconds / 3600);
  if (interval >= 1) {
    return interval + "小时前";
  }
  interval = Math.floor(seconds / 60);
  if (interval >= 1) {
    return interval + "分钟前";
  }
  return "刚刚";
}

/**
 * 数字转万
 * 5000 => 5000
 * 15000 => 1.5万
 * 99999 => 9.9万
 * 1000000 => 99万+
 */
export const num2Thousand = (number) => {
  if (number >= 10000 && number < 990000) {
    return (number / 10000).toFixed(0) + "万";
  } else if (number >= 990000) {
    return "99万+";
  } else {
    return number.toString();
  }
}

/**
 * 手机号中间四位*号代替
 */
export const entryMobile = (mobile) => {
	return (mobile || '').replace(/^(\d{3})\d*(\d{4})$/, '$1****$2')
}

// 姓名加密
export const encryFullName = (val = '') => {
	return val.substr(0, 1) + new Array(val.length).join('*')
}

// 身份证加密
export const encryIDCard = (val = '') => {
	return val.substr(0, 6) + '********' + val.substr(14)
}

/**
 * 是否是微信浏览器
 */
export const isWeiXin = () => {
  return navigator.userAgent.toLowerCase().indexOf('micromessenger') != -1
}

/**
 * 是否是 iOS 设备
 */
export const isIOS = () => {
  return !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
}